package com.murphy.algorithm.likou;

import java.util.HashMap;
import java.util.Map;

/**
 * 不重复字符串最大长度
 * @author dongsufeng
 * @version 4.0
 * @date 2023/3/2 16:29
 */
public class LengthOfLongestSubstring {

    public int lengthOfLongestSubstring(String input){
        int max = 0;
        int start = 0;
        Map<Character,Integer> map = new HashMap<>();
        for (int i = 0; i < input.length(); i++) {
            char curr = input.charAt(i);
            Integer currIndex = map.get(curr);
            if (currIndex != null &&  currIndex >= start){
                max = Math.max(max,i - start);
                //如果有，找到之前的下一个字母作为开始继续统计
                start =  currIndex + 1;
            }
            map.put(curr,i);
        }
        max = Math.max(max,input.length() - start);
        return max;
    }

    public static void main(String[] args) {
        LengthOfLongestSubstring substring = new LengthOfLongestSubstring();
        System.out.println(substring.lengthOfLongestSubstring("abcabcbb"));
    }
}
